/* 
 * 收藏职位和取消收藏请求处理
 */
define(["jquery", "modules/message"], function ($, messagebox) {
   
    var isLogged = 1;
    var init = function (logged,success) {
        $('.favbtn').click(favJob);
        isLogged = logged;
        afterSccess = success;
    };

    var para = { 'before': null, 'onsuccess': null }

    var afterSccess = function () {

    };

    var favJob = function () {
        var button = $(this);
        var ftype = button.data("ftype");
        var job = button.data("job");
        var url = '';

        if (!isLogged) {
            //没有登陆 弹窗提示登陆
            $.fancybox.open("#loginbox_global", { autoSize: true, padding: 0, closeBtn: false });
            return false;
        }
        var host = window.location.host;
        host = "http://www." + host.replace(/[^\.]*\.?([^\.]+\.(com\.cn|org\.cn|net\.cn|[^\.]+))\/?.*/, "$1");
        if (ftype == 1) {
            ftype = 2;
            url = host + '/Project-favjob_ajax-job-' + job;
        }
        else if (ftype == 2) {
            ftype = 1;
            url = host + '/Project-unfavjob_ajax-job-' + job;
        }

        $.ajax({
            type: "get",
            async: false,
            url: url,
            dataType: "jsonp",
            jsonp: "callback",//传递给请求处理程序或页面的，用以获得jsonp回调函数名的参数名(一般默认为:callback)
            jsonpCallback: "callme",//自定义的jsonp回调函数名称，默认为jQuery自动生成的随机函数名，也可以写"?"，jQuery会自动为你处理数据
            success: function (vdata) {
                if (vdata.success == 1) {
                    button.data("ftype", ftype);
                    var fc = parseInt(button.text());
                    if (ftype == 1) {
                        button.text(fc - 1);
                        button.prepend("<i class='fa fa-heart'></i>&nbsp;");
                    }
                    else if (ftype == 2) {
                        button.text(fc + 1);
                        button.prepend("<i class='fa fa-heart red'></i>&nbsp;");
                    }
                    if (afterSccess) {
                        afterSccess(button);
                    }
                }
                else {
                    messagebox.alert({ content: vdata.msg });
                }
                return false;
            },
            error: function () {
                messagebox.alert({ content: "查询失败!" });
            }
        });
    }

    return {
        init: init,
        afterSuccess: afterSccess
    };
});
//$('.favbtn').click(favJob);
//var para={'before':null,'onsuccess':null}
//function setPara(pjson){
//    para=pjson;
//}
